import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

import index from '../layout/index'
import Body from "../components/Body";
import TabPage from "../components/TabPage";
import store from  "../store/index"
import {getusertInfo} from "../api/api";
import animate from "../views/animate";

Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'Home',
    component: Home,
    children: [
      {
        path: "",
        component: Body,
        children:[
          {
            path: "",
            component:TabPage
          },
          {
            path: "TabPage",
            component:TabPage
          },
          {
            path:"HotList",
            component: ()=>import("../components/HotList")
          }
        ]
      }
    ]
  },
  {
    path: '/search',
    component: ()=>import('../views/search'),
    children: [
      {
        path: "searchRes",
        component:()=>import('../components/searchRes')
      }
    ]
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // },
  {
    path:'/RotateDeatil',
    component: ()=>import("../views/RotateDeatil"),
    meta: {
      keepAlive: true
    },
  },
  {
    path:'/NewsListdetail',
    component: ()=>import("../views/NewsListdetail")
  },
  {
    path: '/',
    name: 'aniamte',
    component: animate
  },
  {
    path: '/index',
    component: index,
    children:[
      {
        path:'',
        component:()=>import('../components/Body')
      }
    ]
  },
  {
    path: '/music',
    component: ()=> import('../views/music')
  },
  {
    path: '/login',
    component: ()=> import('../views/login'),
    children: [
      {
        path:'',
        component:()=>import('../components/login1')
      },
      {
        path: 'register',
        component: ()=>import('../components/registered')
      },
      {
        path: 'forget',
        component: ()=>import('../components/forget')
      }
    ]
  },
  {
    path: '/userInfo',
    component: ()=>import('../views/userInfo'),
    meta:{
      login:true,
      keepAlive: true,
    },
    children: [
      {
        path: "",
        component: ()=>import('../components/userInfo1'),
        meta:{
          login:true,
          keepAlive: true
        }
      },
      {
        path: "changeUser",
        component:()=>import('../components/changUser'),
        meta:{
          keepAlive: true
        }

      },
      {
        path: 'collect',
        component:()=>import('../components/collList'),
        meta:{
          keepAlive: true
        }

      }
    ]

  },


]
const router = new VueRouter({
  linkExactActiveClass:'active',
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
let info = getusertInfo
//路由守卫
router.beforeEach((to, from, next) => {
  if(to.meta.login){
    info().then((res)=>{
      // console.log(res)
      if(res.code==0){
        store.commit("login/changeIsLogin",true)
        next()
      }else {
        next('/login')
      }
    })
  }else {
    info().then((res)=>{
      // console.log(res)
      if(res.code==0){
        store.commit("login/changeIsLogin",true)
        next();
      }else {
        store.commit("login/changeIsLogin",false)
        next();
      }

    })

  }
})


export default router
